package com.ptszyxx.popose.module.main.map;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapWrapper;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.constants.BundleConstant;
import com.ptszyxx.popose.databinding.FragmentMapAddressBinding;
import com.ptszyxx.popose.module.main.map.web.MAWebViewWrapper;
import com.ysg.base.BaseFragment;
import com.ysg.base.BaseViewModel;
import com.ysg.http.data.entity.home.HomeResult;
import com.ysg.utils.YBitmapUtil;
import com.ysg.utils.YStringUtil;

/**
 * 地图地址
 */
public class MapAddressFragment extends BaseFragment<FragmentMapAddressBinding, BaseViewModel> {
    private AMap aMap;
    private AMapWrapper mapView;
    private LatLng centerLatLng;
    private String userId, userPic;

    private float currentZoom = 12;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_map_address;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

//    @Override
//    public MapVM initViewModel() {
//        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
//        return ViewModelProviders.of(this, factory).get(MapVM.class);
//    }


    @Override
    public void initParam() {
        super.initParam();
        Bundle bundle = getArguments();
        if (bundle != null){
            String lat = bundle.getString(BundleConstant.LAT);
            String lng = bundle.getString(BundleConstant.LNG);
            userId = bundle.getString(BundleConstant.USER_ID);
            userPic = bundle.getString(BundleConstant.USER_PIC);
            if (YStringUtil.isNotEmpty(lat) && YStringUtil.isNotEmpty(lng)){
                try {
                    centerLatLng = new LatLng(Double.valueOf(lat), Double.valueOf(lng));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void initView() {
        super.initView();
        MAWebViewWrapper webViewWrapper = new MAWebViewWrapper(binding.mapView);
        mapView = new AMapWrapper(getContext(), webViewWrapper);
        mapView.onCreate();

        mapView.getMapAsyn(new AMap.OnMapReadyListener() {
            @Override
            public void onMapReady(AMap map) {
                aMap = map;
                jumpToCenter(centerLatLng);
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }


    /**
     * 跳转到中心点
     */
    private void jumpToCenter(LatLng latLng) {
        if (latLng == null){
            return;
        }
        CameraUpdate update = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, currentZoom, 30, 30));//当前缩放等级15
        aMap.moveCamera(update);
        aMap.clear();

        //添加标记物
        HomeResult data = new HomeResult();
        data.setUserid(userId);
        data.setUsericon(userPic);
        addMarkerFromUrl(data, latLng, true);
    }


    public void addMarkerFromUrl(HomeResult data, LatLng latLng, boolean isSelf){
        YBitmapUtil.getMapBitmap(data.getUsericon(), isSelf, new YBitmapUtil.OnMapBitmapListener() {
            @Override
            public void onSuccess(Bitmap bitmap) {
                BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
                MarkerOptions markerOption = new MarkerOptions().icon(descriptor).position(latLng);
                Marker marker = aMap.addMarker(markerOption);
                marker.setTitle(data.getUserid() + "");
            }

            @Override
            public void onError(Exception e) {
            }
        });
    }

}
